package edu.ccut.saturn.basecomponent.database.impl.sqlserver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.component.IDataInfo;
import edu.ccut.saturn.component.IDataItem;
import edu.ccut.saturn.component.SaturnData;

/**
 * 经refactory后生成的新类,作用:针对sqlserver将生成的主键在SaturnData重新赋值
 * 
 * @author zjh
 * 
 */
public class SqlServerSetPrimaryKeyInSaturnData {
	private static final String LAST_INSERT_ID = "SELECT @@IDENTITY";
	private static final int PRIMARY_KEY_COLUMN_NUMBER = 1;
	/**
	 * @author zjh
	 * @param connection
	 *            数据库连接对象
	 * @param saturnSQL
	 *            saturnSQL对象
	 */
	public static void setPrimaryKeyValue(Connection connection,
			ISaturnSQL saturnSQL) {
		
		if (connection != null && saturnSQL != null) {
			
			ResultSet resultSet = null;
			Statement statement = null;
			
			try {
				statement = connection.createStatement();
				resultSet = statement.executeQuery(LAST_INSERT_ID);
				// 结果唯一,如果没有,则返回0;
				if (resultSet.next()) {
					
					int currPrimaryKeyValue = resultSet.getInt(PRIMARY_KEY_COLUMN_NUMBER);
					
					SaturnData saturnData = saturnSQL.getSaturnData();
					IDataInfo dataInfo = saturnData.getSaturnDataInfo();
					
					for (String key : dataInfo.keySet()) {
						
						IDataItem dataItem = dataInfo.getDataItem(key);
						
						if ("true".equalsIgnoreCase(dataItem.getIsPrimaryKey())) {
							
							saturnData.set(key, currPrimaryKeyValue);
							
							break;
						}
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if (statement != null) {
					try {
						statement.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
				if (resultSet != null) {
					try {
						resultSet.close();
					} catch (SQLException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}

	private static Connection getConnection() {
		final String URL = "jdbc:sqlserver://localhost:1433;DatabaseName=saturn";
		final String USER = "sa";
		final String PASSWORD = "saturn";
		final String DRIVERCLASS = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		Connection connection = null;
		try {
			Class.forName(DRIVERCLASS);
			connection = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return connection;
	}

	public static void main(String[] args) {
		Connection connection = getConnection();
		Statement statement = null;
		Statement statement1 = null;
		PreparedStatement ps = null;
		ResultSet resultSet = null;
		PreparedStatement ps1 = null;
		try {
			if (connection != null) {
				// statement = connection.createStatement();
				// int b = statement.executeUpdate("insert into test (name)
				// values('state')");
				// ps = connection.prepareStatement("insert into test (name)
				// values('zjh')");
				// int b =ps.executeUpdate();
				// System.out.println("b="+b);
				statement1 = connection.createStatement();
				resultSet = statement1.executeQuery("select @@identity");
				// resultSet = statement1.executeQuery("select
				// scope_identity()");
				// ps1 = connection.prepareStatement("select scope_identity()");
				// resultSet = ps1.executeQuery();
				boolean c = resultSet.next();
				System.out.println("c=" + c);
				int a = resultSet.getInt(1);
				System.out.println("a=" + a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (statement1 != null) {
				try {
					statement1.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (resultSet != null) {
				try {
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (ps1 != null) {
				try {
					ps1.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
